<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\book\model;

use manniu\model\Base;

use manniu\controller\Cache;

class ChapterData extends Base
{
    // 只读字段
    protected $readonly = ['chapter_id'];

    // 章节内容缓存名
    protected $chapterDataCacheKey = 'chapter{id}data';
    // 章节内容缓存redis类型
    protected $chapterDataRedisType = 'hash';
    // 章节内容缓存过期时间
    protected $chapterDataCacheTime = 300;

	// 内容 - 获取器
    protected function getContentAttr($value) {
        return htmlspecialchars_decode($value);
    }

    // 模型事件
    protected static function init(){
        // 钩子 - 写入前
        ChapterData::beforeWrite(function ($chapter_data) {
            // 删除章节缓存
            $chapter_data->rmchapterDataCache($chapter_data->getData('chapter_id'));
        });

        // 钩子 - 写入后
        ChapterData::afterWrite(function ($chapter_data) {
            // 生成章节内容缓存
            $chapter_data->setchapterDataCache($chapter_data->getData('chapter_id'));
        });

        // 钩子 - 删除前
        ChapterData::beforeDelete(function ($chapter_data) {
            // 删除章节缓存
            $chapter_data->rmchapterDataCache($chapter_data->getData('chapter_id'));
        });
    }

    /**
     * 获取章节内容数据
     * @param int $id 章节Id
     * @param int|boolean $time sql查询缓存标识/缓存时间
     * @return array|bool|null
     */
    public function getChapterData($id = 0, $time = false){
        $id = (int)$id;
        if(!$id){
            return null;
        }
        $chapter_data = $this->getchapterDataCache($id);
        if(!$chapter_data){
            $chapter_data = $this->setchapterDataCache($id, $time, true);
        }
        return $chapter_data;
    }

    /**
     * 获取章节内容缓存
     * @param int $id 章节id
     * @return bool
     */
    protected function getchapterDataCache($id){
        return Cache::get($this->formatCacheKey($this->chapterDataCacheKey, ['id'=>$id]), $this->chapterDataRedisType);
    }

    /**
     * 删除章节内容缓存
     * @param $id 章节id
     */
    protected function rmchapterDataCache($id){
        Cache::rm($this->formatCacheKey($this->chapterDataCacheKey, ['id'=>$id]));
    }

    /**
     * 生成章节内容缓存
     * @param int $id 章节id
     * @param bool $cache 缓存/缓存时间
     * @param bool $is_return 是否返回查询数据
     * @param array $hidden_field 隐藏属性
     * @return array
     */
    protected function setchapterDataCache($id, $cache = false, $is_return = false, $hidden_field = []){
        $chapter_data = [];
        $data = $this->where(['chapter_id'=>$id])->cache($cache)->find();
        if($data){
            $chapter_data = $data->hidden($hidden_field)->toArray();
            Cache::set($this->formatCacheKey($this->chapterDataCacheKey, ['id'=>$id]), $chapter_data, $this->chapterDataCacheTime, $this->chapterDataRedisType);
        }
        if($is_return === true){
            return $chapter_data ? $chapter_data : null;
        }
    }
}